#include <stdio.h>
#include <string.h>
#include "IPCAM_Export.h"
#include "Daemon_GetNetInfo.h"
#include "Daemon_FacInfoCtl.h"
#include "Daemon_Common.h"

INT  Daemon_Getgw(CHAR *gw_addr)
{
	ip_str_metric metric;
	
	CHAR  buf[255] = {0};
	FILE *fp       = NULL;
	INT   nOK      = 0;
	
	if(NULL == gw_addr)		                       return FALSE;	
	if(NULL == (fp=fopen("/proc/net/route", "r"))) return FALSE;
	
	memset(&metric, 0, sizeof(metric));
	while(!feof(fp)){
		fgets((char *)buf, 254, fp);
		sscanf(buf, "%s %x %x %x %d %d %d %d %d %d %d" , metric.name, 
			&metric.des, &metric.gateway, &metric.flag, 
			&metric.val[0], &metric.val[1], &metric.val[2], &metric.val[3], &metric.val[4], 
			&metric.val[5], &metric.val[6]);
		
		if (metric.flag == 3){
			snprintf(gw_addr, 19,  "%d.%d.%d.%d", 
				metric.gateway & 0xff, metric.gateway>>8 & 0xff,
				metric.gateway>>16 & 0xff, metric.gateway>>24 & 0xff);	
			nOK = 1;
			break;
		}
	}	
	fclose(fp);
	if(1 == nOK)  return TRUE;
	return FALSE;
}

INT Daemon_Getipadr(CHAR *ip_addr)
{
	CHAR  ip_sys[80] = {"ifconfig eth0 | grep inet | cut -d: -f2 | cut -d' ' -f1 > /tmp/ipaddr.txt"};
	FILE *ip_fp      = NULL;
	INT   error_sys  = -1;
	CHAR *delimiter  = NULL;
	
	if((error_sys = hi_system(ip_sys)) !=0){
		IPCAM_DEBUG("[get_eth] ip_sys : 0x%x\n", error_sys);
		return FALSE;
	}
	
	if ((ip_fp=fopen("/tmp/ipaddr.txt", "r"))== NULL){
		IPCAM_DEBUG("open ipaddr.txt is error!");
		return FALSE;
	}
	
	fgets(ip_addr, 39, ip_fp);
	fclose (ip_fp);		
	
	if( (delimiter=strchr(ip_addr, '\x0a')) != NULL)	*delimiter = '\0';
	unlink("/tmp/ipaddr.txt");
	return TRUE;
}

INT Daemon_GetNat(CHAR *nat_addr)
{
	CHAR  ip_sys[256] = {0};
	FILE *ip_fp       = NULL;
	INT	  error_sys   = -1;
	
	if(nat_addr==NULL)		        return FALSE;
	
	memset(ip_sys , 0 , sizeof(ip_sys));
	sprintf(ip_sys , "ifconfig %s | grep inet | cut -d: -f2 | cut -d' ' -f1 > /tmp/ipaddr.txt" , "eth0:1" );
	if((error_sys = hi_system(ip_sys)) !=0)
	{
		IPCAM_DEBUG( "[get_eth] ip_sys : 0x%x\n", error_sys);
		return FALSE;
	}
	if ((ip_fp=fopen("/tmp/ipaddr.txt", "r")) == NULL)
	{
		IPCAM_DEBUG("open ipaddr.txt is error!");
		return FALSE;
	}
	
	fgets(nat_addr, 39, ip_fp);
	fclose (ip_fp);		
	unlink("/tmp/ipaddr.txt");
	return  TRUE;
}


INT Daemon_GetDNS(CHAR *dns_addr )
{
	#if 0
		FILE	*fp 		= NULL;
		CHAR	*buf		= NULL; 
		CHAR	name[80]	= {"\0"};
		size_t 	len 		= 0;
		INT 	read		= 0;
		INT 	iRet		= 0;
		if(NULL == dns_addr){
			return FALSE;
		}
	
		fp = fopen("/etc/resolv.conf", "r");
		if (fp == NULL){
			return FALSE;
		}
		while((read = getline(&buf, &len, fp)) > 0){  //读取第一个域名
			iRet = sscanf(buf, "%s %s", name, dns_addr);
			if (iRet != 2){
				continue;
			}
			if ( !strncasecmp(name, "nameserver", strlen("nameserver"))){
				iRet = TRUE;
				break;
			}			
		 }
		if (iRet == FALSE){
			IPCAM_DEBUG("NET_GetDNS Get First Error");
			goto NET_GetDNS_FALSE;
		}
		dns_addr[15] = '\0';
		while((read=getline(&buf,&len,fp)) > 0) //读取第二个域名
		{
			iRet = sscanf(buf, "%s %s", name, dns_addr+16);
			if (iRet != 2){
				continue;
			}
			if ( !strncasecmp(name, "nameserver", strlen("nameserver"))){
				iRet = TRUE;
				break;
			}
		}
		
NET_GetDNS_FALSE:
		fclose(fp);
		if(NULL != buf){
			free(buf);
		}
		return iRet;
	#endif

}

INT Daemon_GetMacAddr(CHAR *mac_addr)
{
	CHAR  ip_sys[256] = {0};
	FILE *ip_fp       = NULL;
	INT	  error_sys   = -1;
	CHAR *delimiter   = NULL;
	
	if(mac_addr==NULL)	return FALSE;
	memset(ip_sys , 0 , sizeof(ip_sys));
	sprintf( ip_sys , "ifconfig %s | grep HWaddr | cut -dW -f2 | cut -d' ' -f2 > /tmp/ipaddr.txt" , "eth0");
	if((error_sys = hi_system(ip_sys)) !=0)
	{
		IPCAM_DEBUG( "[get_eth] ip_sys : 0x%x\n", error_sys);
		return FALSE;
	}
	if ((ip_fp=fopen("/tmp/ipaddr.txt", "r"))== NULL) 
	{
		IPCAM_DEBUG("open /tmp/ipaddr.txt is error!");
		return FALSE;
	}
	
	fgets(mac_addr, 24, ip_fp);
	fclose (ip_fp);			
	if( (delimiter=strchr(mac_addr, '\x0a')) != NULL)
	{
		*delimiter = '\0';
	}
	unlink("/tmp/ipaddr.txt");
	return TRUE;
}

#include "lwip/netifapi.h"
#include "lwip/ip_addr.h"

INT Daemon_GetNetInfo(LPDM_SYSNETINFO pNetInfo)
{
	CHAR  strDNS[256] = {0};
	int iRet = 0;
	if(NULL  == pNetInfo)		return FALSE;
	iRet = pox_get_macaddr(NET_ETH,(char*)pNetInfo->NetMacAddr);
	if (PoxNetRet_TRUE != iRet){
		IPCAM_DEBUG("pox_get_macaddr ERROR");
	}

	iRet = pox_get_ipaddr(NET_ETH,(char*)pNetInfo->NetIpAddr);
	if (PoxNetRet_TRUE != iRet){
		IPCAM_DEBUG("pox_get_ipaddr ERROR %s   %d", pNetInfo->NetIpAddr, iRet);
	}

	iRet = pox_get_netmask(NET_ETH,(char*)pNetInfo->NetSubMask);
	if (PoxNetRet_TRUE != iRet){
		IPCAM_DEBUG("pox_get_netmask %d ERROR", iRet);
	}

#ifdef __HuaweiLite__
    ip_addr_t gateway;
    struct netif *netif = NULL;
    netif = netifapi_netif_get_default();
    if (NULL != netif) {
        gateway = netif->gw;
        snprintf(pNetInfo->NetGwAddr, 16, "%s", ipaddr_ntoa(&gateway));
    }
    
    NetGetDns((char*)pNetInfo->NetDnsSvI, (char*)pNetInfo->NetDnsSvII);
#else
	iRet = get_gateway((char*)pNetInfo->NetGwAddr);
	if (PoxNetRet_TRUE != iRet){
		IPCAM_DEBUG("pox_get_gateway ERROR");
	}
	Daemon_GetDNS(strDNS);
    strncpy((char*)pNetInfo->NetDnsSvI, strDNS, 16);
    strncpy((char*)pNetInfo->NetDnsSvII, strDNS+16, 16);
#endif /* __HuaweiLite__ */


	return TRUE;
}

INT Daemon_ChangeMacAddr(SAVENANDINFO_T *pFacInfo, const char *ifname ,int dhcp)
{
#if 0
	INT i    = 0;
	INT Size = 0;
	CHAR NewMacAddr[64] = {0};
	CHAR TempMacAddr[26]= {0};
//	CHAR Gateway[17]  	= {0};
	CHAR netName[12] 	= "eth0";
	CHAR *pMac 			= strrchr((char*)pFacInfo->devSerialNumb, '-');

	if( NULL == pMac){
		IPCAM_DEBUG("info of factory is error!");
		return FALSE;
	}

	if(ifname != NULL) strncpy(netName,ifname,12);
	
	strncpy(NewMacAddr, pMac + 1, 12);

	memset (TempMacAddr, 0, sizeof TempMacAddr);
	for (i=0;i<6;i++){
		strncpy(&TempMacAddr[Size], &NewMacAddr[i*2], 2);
		Size += 2;
		strcpy (&TempMacAddr[Size++],  ":");	
	}
	
	TempMacAddr[17] = '\0';
	if(PoxNetRet_TRUE == pox_get_macaddr(netName,(char*)pFacInfo->NetMacAddr)){
		IPCAM_DEBUG("MAC: old[%s]  set[%s]\n", TempMacAddr, pFacInfo->NetMacAddr);
		if(0 == strcasecmp((char*)pFacInfo->NetMacAddr, TempMacAddr)){
			IPCAM_DEBUG("mac addr is the same!");
			return TRUE;
		}
	}
	strncpy((char*)pFacInfo->NetMacAddr, TempMacAddr, 18);

	
	ChangeMacAddr(netName,pFacInfo->NetMacAddr,dhcp);
#endif

	return TRUE;
}

INT SaveEthAttr2File(int type, char *str)
{
	if (str == NULL)
	{
		return 0;
	}
	#define  DATA_LEN 1024
	FILE *fp = NULL;
	char str_type[10];
	char line[32];
	char *buf = NULL;
	int found = 0;

	switch(type)
	{
		case TYPE_IP:
		{
			 strcpy(str_type, "HOSTIP");
			 break;
		}
		case TYPE_NETMASK:
		{
			 strcpy(str_type, "SUBMASK");
			 break;
		}
		case TYPE_GATEWAY:
		{
			 strcpy(str_type, "GATEWAYIP");
			 break;
		}
		default:
			return -1;
	}

	if ((fp = fopen(NET_WORK, "r+")) != NULL)
	{
		//读数据
		buf = (char *)malloc(DATA_LEN);
		if (buf == NULL)
		{
			LOGE("Malloc Failed\n");
			return -1;
		}
		memset(buf, 0, DATA_LEN);

		while(fgets(line, sizeof(line), fp))
		{
			if(strstr(line, str_type) != NULL)
			{
				sprintf(line, "%s = %s\n", str_type, str);
			    found = 1;
			}
			strcat(buf, line);
		}

		if (!found)
		{
			sprintf(line, "%s = %s\n", str_type, str);
			strcat(buf, line);
		}
		fclose(fp);
		//写
		if ((fp = fopen(NET_WORK, "w+")) != NULL)
		{
			fprintf(fp, "%s", buf);
			fclose(fp);
		}
		free(buf);
	}
	else
	{
		if ((fp = fopen(NET_WORK, "w+")) != NULL)
		{
			fprintf(fp, "%s = %s\n", str_type, str);
			fclose(fp);
		}
	}
	return 0;
}

